<template>
    <div class="download_bt" @click="on_download">
        <div class="left">
            <slot name="left"></slot>
        </div>
        <div class="right">
            下载
        </div>
    </div>
</template>
<script>

export default { //下载按钮组件
    name:'Download_bt',
    data(){
        return{
            dialog:null,
        };
    },
    created(){
        this.dialog = require('electron').remote.dialog;
    },
    methods:{
        on_download(){ //文件下载
            if(this.$store.state.selection_files.length){
                this.dialog.showOpenDialog({
                    properties: ['openDirectory'],
                }).then(({canceled,filePaths})=>{
                    if(!canceled){
                        this.$store.commit('set_loading',{
                            state:true,
                            msg:'正在下载',
                        });
                        let path = filePaths[0];
                        let fileNames=this.$store.state.selection_files.map(item=>{
                            return item.fileName || item.hadoopFileName;
                        }) || [];
                        return this.$axios.json_post('file/downloadke',{
                            downName:path+'\\',
                            fileName:fileNames.join(','),
                        });
                    }
                }).then(({data})=>{
                    data.forEach(item => {
                        setTimeout(()=>{
                            this.$store.commit('add_download_file',{
                                file:{
                                    name:item.fileName,
                                },
                                msg:item.msg || '下载成功',
                                state:item.sate==1?'success':'fail',
                            });
                        },0);
                    });
                }).catch(()=>{
                    return;
                }).finally(()=>{
                    this.$store.commit('set_loading',{
                        state:false,
                    });
                });
            }else{
                return;
            }
        },
    },
}
</script>
<style lang="scss" scoped>
.download_bt{
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 14px;
    line-height: 100%;
    cursor: pointer;
    padding: 0 10px;
    box-sizing: border-box;
    >div{
        display: flex;
        justify-content: center;
        align-items: center;
    }
    i{
        width: 24px;
        height: 24px;
        margin-right: 5px;
    }
}
</style>